- /* sdfddiv2.cpp by K.Tsuru */
- // function ID = 327 DRADIX, BRADIX
- /*********************************************
- SDouble and SDecimal classes
- n/2
- It is faster than DsDiv but without large roop
- use DsDiv(n, 2).
- **********************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
- SDouble DDiv2(const SDouble& n){
- uint rf = n.First(), rl = n.Last(); // rl < n.CurrentMaxSize()
- if(n.IsPointFixed() && n.IsHidden() && (rf == rl) && (n.figure(rf) < 2) ){
- return SDZero(n); // |n| == 0.0000 ..... 0001
- }
- SDouble result(n.Type(), 0);
- result.rdxExp = n.rdxExp;
- const fType* np;
- if( rf && (n.figure(rf) == 1) && !n.IsPointFixed()){
- //If first figure is equal to one,it shifts to upper to avoid to lose precision.
- result = n;
- result.ShiftArray(-1); result.rdxExp--;
- rf--; rl--;
- np = result.figure.Elements();
- }else {
- np = n.ReadFigures();
- //do not use result.figure.size(n.figure.size(), -1);
- result.valloc(n.figure.size(), -1);
- if(rf) result.figure.clear(0, rf-1);
- result.figure.clear(rl+1);
- }
- fType* rp = result.figure.Elements();
- fType rem = 0, u; // rem : remainder
- fType rdx = n.Radix();
- uint i;
- #ifndef NDEBUG
- result.figure(rl); n.figure(rl);
- #endif
-
- for(i = rf; i <= rl; i++) {
- u = np[i];
- if( rem ) u += rdx;
- rem = u & 1; // rem = 0 or 1
- rp[i] = u >> 1;
- }
- // rem = 0 or 1
- if(rem && result.Reserve(i)){
- rp = result.figure.Elements();
- rl = i;
- result.figure[i] = rdx/2;//It increases by one figure.
- }
- /******************************************************
- Do not set result.aHead = rl.
- Dividing n = 0.1234 0000 0000 ... 0000 0001 by two gives
- result = 0.0617 0000 0000 ... 0000.
- The last position largely moves to upper.
- *******************************************************/
- while(!rp[rl] && (rl > rf) ) rl--;
- if( (rl == rf) && !rp[rl] ){
- result.SetZero(); return result;
- }
- result.aHead = rl;
-
- while(!rp[rf]) rf++;
- result.aTail = rf;
- result.SetSign(n.Sign(327));
- result.Reform(327);
- return result;
- }
sdfddiv2.cpp : last modifiled at 2017/03/13 14:31:58(2,057 bytes)
created at 2017/10/07 10:22:50
The creation time of this html file is 2017/10/07 11:29:39 (Sat Oct 07 11:29:39 2017).